package com.lanou.moving.adapter;

import android.widget.BaseAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.moving.R;
import com.lanou.moving.entity.MusicInfo;
import com.lanou.moving.entity.SongListDetail;
import com.lanou.moving.utils.Exchange;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by 刘琪 on 15/10/28.
 */
public class SongListDetailAdapter extends BaseAdapter {
    private Context context;
    private List<SongListDetail.SongsEntity> songsEntities;
    private int size;
    private List<MusicInfo> musicInfoList;
    private OnMenuClickListener onMenuClickListener;
    private SongListDetail.SongsEntity songsEntity;

    public SongListDetailAdapter(Context context, List<SongListDetail.SongsEntity> songsEntities) {
        this.context = context;
        this.songsEntities = songsEntities;
    }

    public void setOnMenuClickListener(OnMenuClickListener onMenuClickListener) {
        this.onMenuClickListener = onMenuClickListener;
    }

    @Override
    public int getCount() {
        return songsEntities != null && songsEntities.size() > 0 ? songsEntities.size() : 0;
    }

    public interface OnMenuClickListener {
        void onMenuClick(SongListDetail.SongsEntity songsEntity);
    }

    @Override
    public SongListDetail.SongsEntity getItem(int position) {
        return songsEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.songs_list_listview_item, null);
            holder = new ViewHolder();
            holder.songs_list_position = (TextView) convertView.findViewById(R.id.songs_list_position);
            holder.songs_list_songName = (TextView) convertView.findViewById(R.id.songs_list_songName);
            holder.songs_list_item_singer = (TextView) convertView.findViewById(R.id.songs_list_item_singer);
            holder.songs_list_item_number = (TextView) convertView.findViewById(R.id.songs_list_item_number);
            holder.list_more = (ImageView) convertView.findViewById(R.id.list_more);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final SongListDetail.SongsEntity songsEntity = getItem(position);
        musicInfoList = new ArrayList<>();
        for (SongListDetail.SongsEntity entity : songsEntities) {
            if (entity.getUrlList() != null && entity.getUrlList().size() > 0) {
                final MusicInfo musicInfo = new MusicInfo();
                musicInfo.setSongName(entity.getName());
                musicInfo.setAlbum(entity.getAlbumName());
                musicInfo.setAlbumId((int) entity.getAlbumId());
                musicInfo.setUrl(entity.getUrlList().get(0).getUrl());
                musicInfo.setDuration(entity.getUrlList().get(0).getDuration());
                musicInfo.setSingerName(entity.getSingerName());
                musicInfo.setResouce("网络");
                musicInfoList.add(musicInfo);
            }
        }
        if (songsEntity != null) {
            String num = Exchange.getCount(songsEntity.getFavorites());
            holder.songs_list_position.setText(position + 1 + "");
            holder.songs_list_songName.setText(songsEntity.getName());
            holder.songs_list_item_singer.setText(songsEntity.getSingerName());
            holder.songs_list_item_number.setText(num);
            holder.list_more.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onMenuClickListener != null) {
                        onMenuClickListener.onMenuClick(songsEntities.get(position));
                    }
                }
            });
        }
        return convertView;
    }

    private class ViewHolder {
        private ImageView list_more;
        private TextView songs_list_position;
        private TextView songs_list_songName;
        private TextView songs_list_item_singer;
        private TextView songs_list_item_number;
    }

}
